<template>
  <div v-loading="loading">
    <div class="content-title">
      <span>产品类别设置</span>
    </div>
    <div class="product-setting">
      <div class="tips">注：产品类别最多设置20级</div>
      <div class="product-setting-con">
        <div>
          <span
            class="el-icon-plus"
            style="color: #409EFF;"/>
          <el-button
            type="text"
            @click.native="handleTreeSetDrop({type:'create-one'})">新增一级分类</el-button>
        </div>
        <div class="tree-box">
          <el-tree
            :data="treeData"
            :props="defaultProps"
            default-expand-all>
            <flexbox
              slot-scope="{ node, data }"
              class="node-data">
              <img
                v-if="node.expanded"
                class="node-img"
                src="@/assets/img/fold.png">
              <img
                v-if="!node.expanded"
                class="node-img"
                src="@/assets/img/unfold.png">
              <div class="node-label">{{ node.label }}</div>
              <el-dropdown
                v-if="node.level < maxCreateLevel"
                trigger="click"
                @command="handleTreeSetDrop">
                <div
                  class="node-label-set"
                  @click.stop="getChild(node)">设置</div>
                <el-dropdown-menu slot="dropdown">
                  <el-dropdown-item
                    v-for="(item, index) in treeSetTypes"
                    :key="index"
                    :command="item">{{ item.name }}</el-dropdown-item>
                </el-dropdown-menu>
              </el-dropdown>
            </flexbox>
          </el-tree>
        </div>
      </div>
    </div>
    <el-dialog
      :visible.sync="productHandleDialog"
      :close-on-click-modal="false"
      title="提示"
      width="400px">
      <el-form :model="productForm">
        <el-form-item
          label="类别名称"
          label-width="80">
          <el-input
            :maxlength="20"
            v-model="productForm.name"
            autocomplete="off"/>
        </el-form-item>
      </el-form>
      <div
        slot="footer"
        class="dialog-footer">
        <el-button @click="productHandleDialog = false">取 消</el-button>
        <el-button
          type="primary"
          @click="handleProduct">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  jxcProductQueryProductTypeAPI,
  jxcProductAddOrUpdateProductTypeAPI,
  jxcProductDeleteProductTypeByIdAPI
} from '@/api/jxc/product'

export default {
  name: 'ProductCategorySet',

  components: {},

  data() {
    return {
      loading: false, // 展示加载中效果

      // 产品类别设置
      treeData: [],
      /** 更多操作 */
      treeSetTypes: [],
      // 最大可创建20级
      maxCreateLevel: 20,
      // 编辑产品弹窗
      productHandleDialog: false,
      productForm: { name: '', id: '', pid: '' },
      defaultProps: {
        children: 'sonList',
        label: 'productTypeName'
      }
    }
  },
  created() {
    this.getProductCategoryIndex()
  },
  methods: {
    /**
     * 产品类别设置
     */
    getChild(node) {
      var temps = [
        { type: 'create-child', name: '新建子分类' },
        { type: 'create-brother', name: '新建平级分类' },
        { type: 'edit', name: '编辑分类' },
        { type: 'delete', name: '删除分类' }
      ]
      this.treeSetTypes = temps.map(function(item, index, array) {
        item['node'] = node
        return item
      })
    },

    /**
     * 产品操作
     */
    handleTreeSetDrop(command) {
      this.productForm = {
        id: null,
        name: null,
        pid: null
      }
      console.log('command: ', command)

      let nodeData = {}
      if (command.node) {
        nodeData = command.node.data
      }
      switch (command.type) {
        case 'create-one':
          this.productHandleDialog = true
          break
        case 'create-child':
          this.productForm.pid = nodeData.productTypeId || null
          this.productHandleDialog = true
          break
        case 'create-brother':
          this.productForm.pid = nodeData.productTypePaid || null
          this.productHandleDialog = true
          break
        case 'edit':
          this.productForm.pid = nodeData.productTypePaid || null
          this.productForm.name = nodeData.productTypeName || null
          this.productForm.id = nodeData.productTypeId || null
          this.productHandleDialog = true
          break
        case 'delete':
          this.$confirm('确定删除?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            this.loading = true
            jxcProductDeleteProductTypeByIdAPI({
              id: nodeData.productTypeId
            }).then(() => {
              this.loading = false
              this.$message.success('删除成功')
              this.getProductCategoryIndex()
            }).catch(() => {
              this.loading = false
            })
          }).catch(() => {
            this.$message.info('已取消删除')
          })
          break
      }
    },

    /**
     * 产品类别操作
     */
    handleProduct() {
      if (!this.productForm.name.length) {
        this.$message.warning('请填写名称')
        return
      }
      this.loading = true
      jxcProductAddOrUpdateProductTypeAPI(this.productForm).then(() => {
        this.loading = false
        this.getProductCategoryIndex()
        this.$message.success('操作成功')
        this.productHandleDialog = false
      }).catch(() => {
        this.loading = false
      })
    },

    /**
     * 获取产品分类数据
     */
    getProductCategoryIndex() {
      this.loading = true
      jxcProductQueryProductTypeAPI({
        type: 'tree'
      }).then(res => {
        this.loading = false
        this.treeData = res.data
      }).catch(() => {
        this.loading = false
      })
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.content-title {
  padding: 10px;
  border-bottom: 1px solid #e6e6e6;
}

.content-title > span {
  display: inline-block;
  height: 36px;
  line-height: 36px;
  margin-left: 20px;
}

/* 产品类别设置 */
.product-setting {
  flex: 1;
  display: flex;
  flex-direction: column;
  margin: 30px;
  position: relative;
}
.tree-box {
  overflow: auto;
}
.tree-box /deep/ .el-tree-node__expand-icon {
  display: none;
}
.tree-box /deep/ .el-tree-node__content {
  margin-bottom: 10px;
}
.node-data {
  font-size: 13px;
  .node-img {
    width: 15px;
    height: 15px;
    display: block;
    margin-right: 8px;
  }
  .node-label:hover {
    background-color: #ededed;
  }
  .node-label-set {
    margin-left: 8px;
    font-size: 12px;
    color: $xr-color-primary;
    display: none;
  }
}

.node-data:hover .node-label-set {
  display: block;
}

.product-setting-con {
  position: absolute;
  top: 20px;
  right: 0;
  bottom: 0;
  left: 0;
  overflow: auto;
}

.tips {
  color: #999;
  font-size: 13px;
}
</style>
